package com.wanjia.seal.manager.controller;

import com.alibaba.fastjson.JSONObject;
import com.wanjia.seal.manager.annotation.LoginToken;
import com.wanjia.seal.manager.controller.ex.DataNotFoundException;
import com.wanjia.seal.manager.entity.ChequeTypeTB;
import com.wanjia.seal.manager.mapper.ChequeTypeTBMapper;
import com.wanjia.seal.manager.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/api")
@Api(tags = "票据类型管理")
public class ChequeTypeTBController extends BaseController{

    @Autowired
    ChequeTypeTBMapper chequeTypeTBMapper;

    @ResponseBody
    @PostMapping("/chequetype")
    @ApiOperation(value = "添加票据类型", notes = "添加是否成功")
    @LoginToken
    public JsonResult createChequeTypeTB(@RequestBody ChequeTypeTB chequeTypeTB){
        String chequeID = chequeTypeTB.getChequeID();
        String chequeName = chequeTypeTB.getChequeName();
        Integer displayPos = chequeTypeTB.getDisplayPos();
        String extChequeID = chequeTypeTB.getExtChequeID();
        ChequeTypeTB chequeTypeTB1 = chequeTypeTBMapper.selectById(chequeID);
        if (chequeTypeTB1!=null){
           throw new DataNotFoundException("数据已存在的异常");
        }
        chequeTypeTB.setChequeName(chequeName);
        chequeTypeTB.setDisplayPos(displayPos);
        chequeTypeTB.setExtChequeID(extChequeID);
        chequeTypeTB.setChequeID(chequeID);
        chequeTypeTBMapper.insert(chequeTypeTB);
        return new JsonResult(OK);
    }
    @ResponseBody
    @DeleteMapping("/chequetype/id")
    @ApiOperation(value = "删除票据类型", notes = "删除是否成功")
    @LoginToken
    public JsonResult<Integer> deleteChequeTypeTB(String ChequeID){
        ChequeTypeTB chequeTypeTB = chequeTypeTBMapper.selectById(ChequeID);
        if (chequeTypeTB==null){
            throw new DataNotFoundException("数据不存在");
        }
        int result = chequeTypeTBMapper.deleteById(ChequeID);
        return new JsonResult<>(OK,result);
    }
    @ResponseBody
    @PutMapping ("/chequetype/id")
    @ApiOperation(value = "更新票据类型", notes = "更新是否成功")
    @LoginToken
    public JsonResult updateChequeTypeTB(@RequestBody ChequeTypeTB chequeTypeTB){
        String chequeID = chequeTypeTB.getChequeID();
        String chequeName = chequeTypeTB.getChequeName();
        Integer displayPos = chequeTypeTB.getDisplayPos();
        String extChequeID = chequeTypeTB.getExtChequeID();
        ChequeTypeTB chequeTypeTB1 = chequeTypeTBMapper.selectById(chequeID);
        if (chequeTypeTB1==null){
            throw new DataNotFoundException("数据不存在");
        }
        chequeTypeTB.setChequeName(chequeName);
        chequeTypeTB.setDisplayPos(displayPos);
        chequeTypeTB.setExtChequeID(extChequeID);
        chequeTypeTBMapper.updateById(chequeTypeTB);
        return new JsonResult(OK);
    }
    @ResponseBody
    @GetMapping("/chequetype/id")
    @ApiOperation(value = "用类型ID查询票据类型", notes = "ID查询是否成功")
    @LoginToken
    public JsonResult<ChequeTypeTB> selectChequeTypeTB(String ChequeID){
        ChequeTypeTB chequeTypeTB = chequeTypeTBMapper.selectById(ChequeID);
        if (chequeTypeTB==null){
            throw new DataNotFoundException("数据不存在");
        }
        return new JsonResult<>(OK,chequeTypeTB);
    }
    @ResponseBody
    @GetMapping("/chequetype")
    @ApiOperation(value = "查询全部票据类型", notes = "查询是否成功")
    @LoginToken
    public JsonResult<Object> selectsChequeTypeTB(Integer pageNum, Integer pageSize){
        List<ChequeTypeTB> chequeTypeTBS = chequeTypeTBMapper.SelectChequeTypeTBPro(pageNum, pageSize);
        Long rows = chequeTypeTBMapper.selectCount(null);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total",rows);
        jsonObject.put("ChequeTypeTB",chequeTypeTBS);
        return new JsonResult<>(OK,jsonObject);
    }
}
